$(function() {
	require(['fileInput','fileInput_zh']);
	var loadList = function () {
		$.get('/file/list',{},function(data){
			let lis = "";
			$.each(data,function(index, node){
				lis += '<li><span class="folder {0}"></span><span class="folder-font"><a href="#">{1}</a></span></li>'.format(node.suffix, node.fileName);
			});
			$("#files_list").html(lis);
			toastr.success('文件上传成功');
			dialog.close();
		});
	};
	$("#file_add .upload-file").click(function(){
		BootstrapDialog.show({
			title:'文件上传',
			size: BootstrapDialog.SIZE_WIDE,
			backdrop : true,
			message : $('<div></div>').load('/fashion/file/upload?url='+encodeURI('/file/list/upload')),
			buttons : [
				{
					id : 'button',
					label : '上传',
					cssClass : 'btn-primary',
					action : function(dialog) {
						$("#sys_file_upload").fileinput("upload");
						$("#sys_file_upload").on("fileuploaded", function(event, data, previewId, index) {
							let cIndex = data.filescount - 1;
							if(cIndex == index){
								loadList();
							}
						});
					}
				}, 
				{
					label : '取消',
					action : function(dialog) {
						dialog.close();
					}
				}
			]
		});
	});
	
	$("#file_delete").click(function(){
		$.confirm("确定删除该文件嘛",function(){
			$.post("/resource/delete", {id : id}, function(data) {
				toastr.result(data,function(){
					loadList();
				});
			});
		});
	});
	//文件绑定悬浮事件
	$("#files_list").on('mouseover','li',function(e){
		$(this).siblings().removeClass('selectli');
		$(this).addClass('selectli');
		if($(this).attr('data-toggle') == undefined){
			$(this).attr("data-toggle","context");
			$(this).attr("data-target","#context-menu");
		}
	});
	
	$("#files_list").on('click','li',function(e){
		$(this).siblings().removeClass('selectli');
		$(this).addClass('selectli');
	});

	$("#file_add").on('click','li',function(e){
//		createNode();
	});
	
//	createNode()
});

